const PNG = require("pngjs").PNG;
const fs=require('fs')
function getGrayData(imageData){
    const grayData={
        width:imageData.width,
        height:imageData.height,
        data:[]
    }
    for(let y=0;y<imageData.height;y++){
        for(let x=0;x<imageData.width;x++){
            const k=4*imageData.width*y+4*x
            grayData.data.push(parseInt((imageData.data[k]+imageData.data[k+1]+imageData.data[k+2])/3));
        }
    }
    return grayData;
}
function getV(x,y,grayData) {
    return grayData.data[y*grayData.width+x];
}
const buff=fs.readFileSync('./Arial16.png');
const imageData=PNG.sync.read(buff)
const grayData=getGrayData(imageData)

function getRectArr(grayData){
    let xArr=new Array(grayData.width)
    let yArr=new Array(grayData.height)

    for(let y=0;y<grayData.height;y++){
        for(let x=0;x<grayData.width;x++){
            const v=getV(x,y,grayData)
            if(!xArr[x]){xArr[x]=[]}
            if(!yArr[y]){yArr[y]=[]}
            if(xArr[x].length<2&&xArr[x].indexOf(v)===-1){
                xArr[x].push(v)
            }
            if(yArr[y].length<2&&yArr[y].indexOf(v)===-1){
                yArr[y].push(v)
            }
        }
    }
    xArr=xArr.map((arr)=>arr.length)
    yArr=yArr.map((arr)=>arr.length)

    let len=0
    const pxArr=[]
    xArr.forEach(function (num,x){
        if(num===1){
            if(len>0){
                pxArr.push([x-len,len])
                len=0
            }
        }else{
            len++
        }
    })
    len=0
    const pyArr=[]
    yArr.forEach(function (num,y){
        if(num===1){
            if(len>0){
                pyArr.push([y-len,len])
                len=0
            }
        }else{
            len++
        }
    })
    const rectArr=[]
    pyArr.forEach(function ([y,h]){
        pxArr.forEach(function ([x,w]){
            rectArr.push([x,y,w,h])
        })
    })
    return rectArr
}
function getMinRectArr(rectArr){
    const minRectArr=[]
//getMin
    rectArr.forEach(function ([x,y,w,h]){
        const v1=getV(x-1,y,grayData)
        let x1=x;
        let x2=x+w;
        let y1=y;
        let y2=y+h;
        let isRunning=true
        while(x1<x2&&isRunning){
            for(let y3=y1;y3<y2;y3++){
                const v2=getV(x1,y3,grayData)
                if(v1!==v2){
                    isRunning=false
                    break;
                }
            }
            if(isRunning){
                x1++
            }
        }
        isRunning=true
        while(x1<x2&&isRunning){
            for(let y3=y1;y3<y2;y3++){
                const v2=getV(x2-1,y3,grayData)
                if(v1!==v2){
                    isRunning=false
                    break;
                }
            }
            if(isRunning){
                x2--
            }
        }
        isRunning=true
        const w1=x2-x1
        if(w1===0){return}
        while(y1<y2&&isRunning){
            for(let x3=x1;x3<x2;x3++){
                const v2=getV(x3,y1,grayData)
                if(v1!==v2){
                    isRunning=false
                    break;
                }
            }
            if(isRunning){
                y1++
            }
        }
        isRunning=true
        while(y1<y2&&isRunning){
            for(let x3=x1;x3<x2;x3++){
                const v2=getV(x3,y2-1,grayData)
                if(v1!==v2){
                    isRunning=false
                    break;
                }
            }
            if(isRunning){
                y2--
            }
        }

        const h1=y2-y1
        if(w1>h1){
            const d=w1-h1
            const ha=d%2===1?(d-1)/2:d/2
            y1=y1-ha
            y2=y2+ha
        }
        minRectArr.push([x1,y1,x2-x1,y2-y1])
    })
    return minRectArr
}
//获取 rectArr
const rectArr=getRectArr(grayData)
//获取 minRectArr
const minRectArr=getMinRectArr(rectArr)
function changeVTest(x,y,imageData) {
    let k=4*imageData.width*y+4*x;
    imageData.data[k]=0;
    imageData.data[k+1]=0;
    imageData.data[k+2]=0;
    imageData.data[k+3]=255;
}
function saveImageToFileTest(filename,imageData) {
    var buffer = PNG.sync.write(imageData, {filterType: 4});
    fs.writeFileSync(filename,buffer)
}
function showTest(rectArr){
    rectArr.forEach(function ([x,y,w,h]){
        for(let y2=y;y2<y+h;y2++){
            changeVTest(x,y2,imageData)
            changeVTest(x+w-1,y2,imageData)
        }
        for(let x2=x;x2<x+w;x2++){
            changeVTest(x2,y,imageData)
            changeVTest(x2,y+h-1,imageData)
        }
    })

    saveImageToFileTest('test.png',imageData)
}
showTest(minRectArr)
fs.writeFileSync('minRectArr.json',JSON.stringify(minRectArr))
// console.log(rectArr)
